function AutoSelector(options) 
{
	// Create a new input element
	var element = $.create('input', {"class":"autocomplete"});
	var selected = null;
	
	// Store script and parameters
	var script = (options.script) ? options.script : null;
	var params = (options.params) ? options.params : {};
	var callback = (options.callback) ? options.callback : null;
	
	// Attach id and class if needed
	if (options.id) $(element).attr("id", options.id);
	if (options['class']) $(element).attr("class", options['class']);
	
	// Self reference (is there any other way to do this?)
    var self = this; 

	// Setup the element to setup autocompletion on focus
	$(element).focus( function() {
    	$(this).autocomplete({ ajax_get: getItemList,
    						   callback: onSelect,
    						   minchars: 2 });
    });
    
    // Trigger on enter key
    $(element).keyup( function(e) {
        
        // No reason to do anything if there is no callback
        if (callback == null) {
            return;
        }
        
        if (e.keyCode == 13) {
        
            if (selected != null) {
                callback(self.getId());
                element.val("");
                selected = null;
            } else if (element.val() != "") {
                getItemList(element.val(), 
                            function(results) {
                                if (results.length > 0) {
                                    callback(results[0].id);
                                    element.val("");
                                    selected = null;
                                }
                            });
            }
        }
    });

    function onSelect(suggest) 
    {	
    	selected = suggest.id;
    	
    	// No reason to do anything if there is no callback
        if (callback == null) {
            return;
        }
        
        callback(self.getId());
        element.val("");
        selected = null;
    }
	
	function getItemList(key, cont) 
	{
		var script_name = script;
   		var script_params = params;
   		script_params.q = key;
   		
   		$.post(	script_name,
   				script_params,
         		function(response) { 
         			if (response.status != 200) {
						alert(response.message);
					}
					
         			var result = [];
         			
					$.each( response.data, function(i, item) {
      					result.push({ id:    item.id, 
      								  value: item.name,
      								  info:  item.info });
    				});
    				
	           		cont(result); 
	           		
	       		},
	       		'json');
	}
	
	this.setParam = function(name, value) 
	{
		params[name] = value;
	}
	
	//
	this.getElement = function()
	{
		return element;
	}
	
	this.getId = function() 
	{
		return selected;
	}
}